//
//  chatV.m
//  testVCtoVC
//
//  Created by Lenny on 16/4/23.
//  Copyright © 2016年 Lenny. All rights reserved.
//

#import "ChatV.h"
#import "ChatListModel.h"
#import "ChatModel.h"

#import "ChatVideoFCell.h"
#import "ChatVideoTCell.h"

@interface ChatV ()<UITableViewDataSource,UITableViewDelegate>
{
    CGFloat _headHigh;
}
@property(nonatomic,strong)UITableView *chatTV;
@property(nonatomic,strong)NSMutableArray *chatA;

@end

@implementation ChatV

- (void)addUI {
    
    [self setData];
    [self addSubview:self.chatTV];
    
    [self.chatTV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
}

#pragma mark - private methods

- (void)setData {
    
    _headHigh = 30;
    self.chatA = [NSMutableArray array];
    for (int i = 0; i < 6; i ++) {
        
        ChatListModel *chatListModel = [[ChatListModel alloc] init];
        chatListModel.timeS = @"14:35";
        for (int k = 0; k < 3; k ++) {
            ChatModel *chatModel = [[ChatModel alloc] init];
            if (k % 2 == 0 && i % 3 == 0) {
                chatModel.messType = MessageTypeText;
                chatModel.contentText = @"内容是否就是饭卡世纪福克斯";
            }else {
                chatModel.messType = MessageTypeVideo;
                chatModel.contentText = @"3s";
            }
            if (k % 4 == 0 && i % 3 == 0) {
                chatModel.sourceType = SourceTypeT;
                chatModel.contentText = @"43s";
            }else {
                chatModel.sourceType = SourceTypeF;
            }
            
            [chatListModel.contentA addObject:chatModel];
        }
        [self.chatA addObject:chatListModel];
    }
    
    [self goBottom];
}

- (void)goBottom {
    
    ChatListModel *chatListModel = self.chatA[self.chatA.count - 1];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:chatListModel.contentA.count - 1 inSection:self.chatA.count - 1];
    [self.chatTV scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark - tableViewDelegate 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.chatA.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    ChatListModel *chatListModel = self.chatA[section];
    return chatListModel.contentA.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChatListModel *chatListModel = self.chatA[indexPath.section];
    ChatModel *chatModel = chatListModel.contentA[indexPath.row];
    
    if (chatModel.sourceType == SourceTypeT) {
        
        NSString *cellTID = @"cellT";
        ChatVideoTCell *cellT = [tableView dequeueReusableCellWithIdentifier:cellTID];
        if (cellT == nil) {
            
            cellT = [[ChatVideoTCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellTID];
            cellT.backgroundColor = [UIColor clearColor];
            [cellT addUI];
        }
        ChatListModel *listM = self.chatA[indexPath.section];
        cellT.chatModel = listM.contentA[indexPath.row];
        return cellT;
        
    }else {
        
        NSString *cellFID = @"cellF";
        ChatVideoFCell *cellF = [tableView dequeueReusableCellWithIdentifier:cellFID];
        if (cellF == nil) {
            
            cellF = [[ChatVideoFCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellFID];
            cellF.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 44);
            cellF.backgroundColor = [UIColor clearColor];
            [cellF addUI];
        }
        ChatListModel *listM = self.chatA[indexPath.section];
        cellF.chatModel = listM.contentA[indexPath.row];
        return cellF;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    ChatListModel *chatListModel = self.chatA[section];
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), _headHigh)];
    headV.backgroundColor = [UIColor clearColor];
    UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, headV.frame.size.height)];
    nameL.text = chatListModel.timeS;
    nameL.textAlignment = NSTextAlignmentCenter;
    nameL.center = CGPointMake(CGRectGetWidth(headV.frame) / 2, nameL.center.y);
    [headV addSubview:nameL];
    return headV;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChatListModel *listM = self.chatA[indexPath.section];
    ChatModel *chatModel = listM.contentA[indexPath.row];
    CGSize size1 = [chatModel.contentText boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    return 50 + size1.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return _headHigh;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

}


#pragma mark - getters and setters
- (UITableView *)chatTV {
    
    if (_chatTV == nil) {
        
        _chatTV = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
        _chatTV.delegate = self;
        _chatTV.dataSource = self;
        _chatTV.separatorColor = [UIColor clearColor];
    }
    return _chatTV;
}

@end
